import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by zyl
 * @date 2020/3/28.
 */
public class Registry {
    private static ECPoint P;
    private static BigInteger s;
    private static ECCOperator ec;

    static {
        ec=new ECCOperator();
        ECPoint G=new ECPoint(new BigInteger("2"),new BigInteger("4"));
        P=ec.multiply(G,new BigInteger("123456"));
        s=new BigInteger(FileUtil.readTag(CommonValue.KeyTable,"KGC").split(" ")[1]);
    }

    public static void sendKeys(){
        while(true){
            String received=SocketUtils.in(CommonValue.KGC_PORT);
            System.out.println("Receiving Regist request:"+received);
            String Eck=received.split(":")[0];
            String RC=received.split(":")[1];
            ECPoint kc=ec.multiply(new ECPoint(new BigInteger(RC.split(",")[0]),new BigInteger(RC.split(",")[1]) ),s);
            System.out.println("Computing kc:"+kc.toString());
            String xOREck=Base64.decode(Eck);
            String uIDcij=XorUtils.xor(xOREck,kc.toString());
            String idline=FileUtil.readID(uIDcij);
            System.out.println("Checking ID from table:"+idline);
            if(!idline.equals("")){
                String RK=CharacterUtils.getRandomInteger(16);
                System.out.println("Generateing RK:"+RK);
                String Ki=HashUtils.Hash(s.toString()+RK+uIDcij);
                System.out.println("Generateing Ki:"+Ki);
                String tag=idline.split(" ")[0];
                Map<String,String> keyMap=new HashMap<String,String>();
                keyMap.put(tag,Ki);
                FileUtil.writeLine(CommonValue.KeyTable,keyMap);
                if(idline.substring(0,1).equals("C")){
                    String ID=FileUtil.readTag(CommonValue.IDTable,"B1").split(" ")[1];
                    String Ekc=Base64.encode(XorUtils.xor((ID+":"+Ki),kc.toString()));
                    SocketUtils.out(CommonValue.KGC_C_IP,CommonValue.KGC_C_PORT,Ekc);
                }
                else if(idline.substring(0,1).equals("B")){
                    String ID=FileUtil.readTag(CommonValue.IDTable,"A").split(" ")[1];
                    String Ekc=Base64.encode(XorUtils.xor((ID+":"+Ki),kc.toString()));
                    SocketUtils.out(CommonValue.KGC_B_IP,CommonValue.KGC_B_PORT,Ekc);
                }
                else if(idline.substring(0,1).equals("A")){
                    String IDC=FileUtil.readTag(CommonValue.IDTable,"C11").split(" ")[1];
                    String IDB=FileUtil.readTag(CommonValue.IDTable,"B1").split(" ")[1];
                    String KC11=FileUtil.readTag(CommonValue.KeyTable,"C11").split(" ")[1];
                    String KB1=FileUtil.readTag(CommonValue.KeyTable,"B1").split(" ")[1];
                    String Ekc=Base64.encode(XorUtils.xor((IDC+":"+IDB+":"+KC11+":"+KB1+":"+Ki),kc.toString()));
                    SocketUtils.out(CommonValue.KGC_A_IP,CommonValue.KGC_A_PORT,Ekc);
                }
            }
        }
    }

    public static void main(String args[]){
        sendKeys();
    }

}